package ink.xiaobaibai.valited;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @description:
 * @author: 小白白
 * @create: 2021-05-22
 **/

public class DoubleValueConstraint implements ConstraintValidator<DoubleSize, Double> {

    private double min;
    private double max;

    @Override
    public void initialize(DoubleSize constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(Double aDouble, ConstraintValidatorContext constraintValidatorContext) {
        return aDouble >= this.min && this.max >= aDouble;
        /**
         * 也可以多重逻辑判断返回不同的错误原因
         */
//        //禁止默认消息返回
//        constraintValidatorContext.disableDefaultConstraintViolation();
//        //自定义返回消息
//        constraintValidatorContext.buildConstraintViolationWithTemplate("参数范围不正确").addConstraintViolation();
//        return false;
    }

}
